package com.zhuimeng.Application.contorller;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.zhuimeng.Application.entity.message;
import com.zhuimeng.Application.entity.user;
import com.zhuimeng.Application.service.IUserSelvice;
import com.zhuimeng.Application.utils.MapCodeUils;
import org.apache.catalina.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Autowired
    private DefaultKaptcha defaultKaptcha;

    @Autowired
    private IUserSelvice userSelvice;

    private Map<String, String> codeMap = new HashMap<>();

    @RequestMapping("/text")
    public void text() {
        System.out.println("success");
        userSelvice.sendEmailByUserName("123");
    }

    /**
     * 用户登录
     *
     * @param username
     * @param userpassword
     * @param code
     * @param codekey
     * @return
     */
    @RequestMapping("/login")
    public message login(String username, String userpassword, String code, String codekey,HttpServletRequest request) {
        HttpSession session= request.getSession(true);
        if (!codeMap.get(codekey).equals(code)) {
            return message.returnmessage("0", "验证码错误");
        }
        user user = userSelvice.login(username, userpassword);
        if (user == null) {
            return message.returnmessage("100", "用户名或密码错误");
        }
        session.setAttribute("user",user);
        return message.returnmessage("200", "登录成功").add("user", user);
    }

    /**
     * 用户注册
     *
     * @param user
     * @return
     */
    @RequestMapping("/register")
    public message register(user user) {
        user.setHeader("123");
        int result = userSelvice.regist(user);
        if (result == -1) {
            return message.returnmessage("0", "账户名已被占用");
        }
        if (result == -2) {
            return message.returnmessage("100", "手机号已被注册");
        }
        return message.returnmessage("200", "注册成功");
    }

    @RequestMapping("/code")
    public void createCode(HttpServletResponse response, String codeKey) throws IOException {
        String code = defaultKaptcha.createText();
        codeMap.put(codeKey, code);
        BufferedImage image = defaultKaptcha.createImage(code);
        ImageIO.write(image, "jpg", response.getOutputStream());
    }

    @RequestMapping("/sendEmailCode")
    public int sendEmailCode(String username) {
        int result = userSelvice.sendEmailByUserName(username);
        return result;
    }

    @RequestMapping("/updatePassword")
    public int updatePassword(String username, String newpassword, String code) {
        String oldCode = MapCodeUils.getCode(username);
        if (oldCode == null) {
            return -1;
        }
        if (!oldCode.equals(code)) {
            return -2;
        }
        int result = userSelvice.updataPassword(username, newpassword);

        MapCodeUils.removeCode(username);
        return result;
    }
}
